package com.example.teamtribe.main.mine.setting.certify;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Code;
import com.example.teamtribe.utils.Utility;

public class CertifyActivity extends BaseActivity implements View.OnClickListener, CertifyContract.View{

    private LinearLayout llFill;
    private TextView tvMeg;
    private EditText etNo;
    private EditText etPassword;
    private EditText etCode;
    private ImageView ivCode;
    private CertifyContract.Presenter presenter;
    private String sessionId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_certify);

        ((TextView) findViewById(R.id.tv_title)).setText("教务网验证");
        llFill = findViewById(R.id.ll_fill);
        tvMeg = findViewById(R.id.tv_meg);
        etNo = findViewById(R.id.et_no);
        etPassword = findViewById(R.id.et_password);
        etCode = findViewById(R.id.et_code);
        ivCode = findViewById(R.id.iv_code);
        presenter = new CertifyPresenter(this);

        ivCode.setOnClickListener(this);
        findViewById(R.id.bt_csust).setOnClickListener(this);

        //initView();
        Glide.with(this).load(R.drawable.fakecode).into(ivCode);
        User user = Utility.getUser();
        user.setCla(3);
        user.setAcademy("计算机与通信工程学院");
        user.setGender(1);
        user.setBirthday("2018-02-23");
        user.setSchoolId(1);
        user.setGrade(2016);
        user.setMajor("软件工程");
        user.setNo("201616080318");
        user.saveOrUpdate();
        Utility.setUser(user);
    }

    private void initView() {
        //if (Utility.getUser().getSchoolId() != 0) {
            //setView("教务网已验证");
        //} else {
            updateCode();
        //}
    }

    @Override
    public void updateCode() {
        presenter.getCodeImage(1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_csust:
                String no = etNo.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                presenter.sendIdentify(1, no, password, code, sessionId);
                break;

            case R.id.iv_code:
                updateCode();
                break;

            default:
                break;
        }
    }

    @Override
    public void setCodeImage(final byte[] image, String sessionId) {
        this.sessionId = sessionId;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide.with(CertifyActivity.this).load(image).into(ivCode);
            }
        });
    }

    @Override
    public void setView(final String meg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                llFill.setVisibility(View.GONE);
                tvMeg.setText(meg);
                tvMeg.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void setResult() {
        setResult(RESULT_OK);
    }
}
